function printright(y,text,Color)
screen:print(480-(string.len(text))*8,y,text,Color)
end
function printmiddle(x,y,text,Color,Image)
Image:print(x-(string.len(text))*8/2,y-4.5,text,Color)
end
function screenalert(time,text,color,clearcolor)
screen:clear(clearcolor)
printmiddle(480/2,272/2,text,color,screen)
screen.waitVblankStart()
screen.flip()
System.sleep(time)
end
function drawbox(x1,y1,x2,y2,kolor,Image)
Image:drawLine(x1, y1, x2, y1, kolor) Image:drawLine(x1, y2, x2, y2, kolor) Image:drawLine(x1, y1, x1, y2, kolor) Image:drawLine(x2, y1, x2, y2, kolor)
end

function polygonangle(sides)
  if sides>1 then
    local rsides = sides*2
    return (180*(rsides-2))/rsides
  end
end
function drawstarredpolygon(pix,piy,os,sides,Image,kolor)
  local xy = {}
  if sides>1 then
    for t = 0 , 239 do
      xy[t] = { x=math.cos(pi / sides * t) * os, y=math.sin(pi / sides * t) * os }
    end
    for t = 0 , 239 do
      for u = 0 , 239 do
        Image:drawLine(pix+xy[t].x,piy+xy[t].y, pix+xy[u].x,piy+xy[u].y, kolor)
      end
    end
  end
end
function boxarea(x1,y1,x2,y2)
return math.abs((x2-x1)*(y2-y1))
end
function boxperimeter(x1,y1,x2,y2)
return math.abs((y2-y1+x2-x1)*2)
end
function drawpolygon(pix,piy,os,sides,Image,kolor)
if sides>1 then
  for t = 0 , (60 * 4 - 1) do
    x = math.cos(pi / sides * t) * os
    y = math.sin(pi / sides * t) * os
    x2 = math.cos(pi / sides * (t+1)) * os
    y2 = math.sin(pi / sides * (t+1)) * os
    Image:drawLine(pix+x, piy+y, pix+x2, piy+y2, kolor)
  end
end
end
function mousecollision(x,y,width,height)
if (mouse.x > x) and (mouse.x < x + width) and (mouse.y > y) and (mouse.y < y + height) then return true end
end
function circumference(radius)
return math.abs(2*pi*radius)
end
function circlearea(radius)
return math.abs(pi*radius*radius)
end
function distance(x1,y1,x2,y2)
  if (((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2))) == 0 then
    return 0
  else
    return math.sqrt( ((x1-x2)*(x1-x2)) + ((y1-y2)*(y1-y2)) )
  end
end
function slope(x1,y1,x2,y2)
  if x2-x1 == 0 then
    return 0
  else
    return ((y2-y1)/(x2-x1))
  end
end
function drawmouse(x, y, Color, sizex, sizey)
screen:drawLine(x - sizex/2, y, x + sizex/2, y, Color)
screen:drawLine(x, y - sizey/2, x, y + sizey/2, Color)
end
function drawgrid(xdistance,ydistance,Image,Color)
for n=0,480/xdistance do
Image:drawLine(n*xdistance,0,n*xdistance,272,Color)
end
for n=0,272/ydistance do
Image:drawLine(0,n*ydistance,480,n*ydistance,Color)
end
end
function drawcircle(pix,piy,os,kolor,Image)
  for t = 0 , (60 * 4 - 1) do
    x = math.cos(pi / 60 * t) * os
    y = math.sin(pi / 60 * t) * os
    x2 = math.cos(pi / 60 * (t+1)) * os
    y2 = math.sin(pi / 60 * (t+1)) * os
    Image:drawLine(pix+x, piy+y, pix+x2, piy+y2, kolor)
  end
end
function snap()
  for n=0,480/grid.xdistance do
    for o=0,272/grid.ydistance do
      if mousecollision(n*grid.xdistance-grid.xdistance/6,o*grid.ydistance-grid.ydistance/6,grid.xdistance/3,grid.ydistance/3) then
        mouse.x,mouse.y=n*grid.xdistance,o*grid.ydistance
      end
    end
  end
end

function copyImage(sourcex, sourcey, width, height)
copiedImage = Image.createEmpty(math.abs(width), math.abs(height))
copiedImage:blit(0, 0, grid.Image, sourcex, sourcey, math.abs(width), math.abs(height))
end


